<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-row>
        <el-col :span="12">
          <el-form-item label="CODE" prop="entityIdentify">
            <el-input v-model="dataForm.sdmCode" placeholder="CODE"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="entityName">
            <el-input v-model="dataForm.sdmName" placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item  label="备注" prop="remark">
        <el-input  type="textarea"  :rows="2" v-model="dataForm.sdmRemark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>

    <div>
      <div class="title">详细</div>
      <el-row style="margin-bottom: 10px;">
        <el-col :span="4">
          <el-button type="primary" @click="addItem()">新增</el-button>
        </el-col>
      </el-row>

      <el-form  ref="dataItemForm" :model="itemDataForm"  label-width="80px">

      <el-table
        :data="itemDataForm.itemData"
        height="250"
        border
        style="width: 100%">
        <el-table-column
          prop="sddCode"
          label="Code"
          width="80">
          <template slot-scope="scope">
            <el-form-item
              :rules="itemRule.sddCode"
              :prop="'itemData.'+scope.$index+'.sddCode'"
            >
              <el-input  v-model="scope.row.sddCode" placeholder="Code"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          prop="sddName"
          label="名称"
          width="100">
          <template slot-scope="scope">
            <el-form-item
              :rules="itemRule.sddName"
              :prop="'itemData.'+scope.$index+'.sddName'"
            >
            <el-input v-model="scope.row.sddName" placeholder="名称"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          prop="sddValue1"
          label="值1">
          <template slot-scope="scope">
            <el-form-item class="tb-colum">
              <el-input v-model="scope.row.sddValue1" placeholder="值1"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          prop="sddValue2"
          label="值2">
          <template slot-scope="scope">
            <el-form-item class="tb-colum">
              <el-input v-model="scope.row.sddValue2" placeholder="值2"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          prop="sddValue3"
          label="值3">
          <template slot-scope="scope">
            <el-form-item class="tb-colum">
              <el-input v-model="scope.row.sddValue3" placeholder="值3"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          prop="sddSeq"
          label="排序">
          <template slot-scope="scope">
            <el-form-item class="tb-colum">
              <el-input type="number" v-model="scope.row.sddSeq" placeholder="排序"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small"  @click.native.prevent="deleteItem(scope.$index, itemDataForm.itemData)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { saveOrUpdate, getInfo } from '@/api/dict'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: null,
          sdmCode: '',
          sdmName: '',
          sdmRemark: ''
        },
        entitys: [],
        entitysProperties: [],
        dataRule: {
          sdmName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          sdmCode: [
            { required: true, message: 'CODE不能为空', trigger: 'change' }
          ]
        },
        itemDataForm: {
          itemData: []
        },
        itemRule: {
          sddName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          sddCode: [
            { required: true, message: 'CODE不能为空', trigger: 'change' }
          ]
        }
      }
    },
    created () {
      this.getEntityCls()
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            let params = {
              id: this.dataForm.id + ''
            }
            getInfo(params).then(res => {
              if (res.data.data && res.data.resultCode === 0) {
                this.dataForm = res.data.data.main
                this.itemDataForm.itemData = res.data.data.details
              }
            })
          }
        })
      },

      // 新增属性
      addItem () {
        let item = {
          sdmCode: '',
          sddCode: '',
          sddName: '',
          sddValue1: '',
          sddValue2: '',
          sddValue3: '',
          sddSeq: 0,
          sddRemark: ''
        }
        this.itemDataForm.itemData.push(item)
      },

      deleteItem (index, rows) {
        rows.splice(index, 1)
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$refs['dataItemForm'].validate((valid1) => {
              if (valid1) {
                let params = {
                  main: JSON.stringify(this.dataForm),
                  details: JSON.stringify(this.itemDataForm.itemData)
                }
                saveOrUpdate(params).then(res => {
                  if (res.data && res.data.resultCode === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.dataForm = {
                          id: null,
                          sdmCode: '',
                          sdmName: '',
                          sdmRemark: ''
                        }
                        this.itemDataForm.itemData = []
                        this.$emit('refreshDataList')
                      }
                    })
                  } else {
                    this.$message.error(res.data.resultMsg)
                  }
                })
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .title{
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }
  .el-table__row .el-form-item__content{
    margin-left: 0 !important;
  }
  .el-table__row .el-form-item{
    margin-bottom: 0 !important;
  }
</style>
